package cqrtplm.aop;

import com.hustcad.plm.pdm.classification.model.dto.TyplmClassificationEntityDTO;
import com.hustcad.plm.pdm.classification.model.dto.TyplmClassificationMoveParamDTO;
import com.hustcad.plm.pdm.classification.model.dto.TyplmClassificationQueryTreeDTO;
import com.hustcad.plm.pdm.classification.model.vo.TyplmClassificationEntityTreeVO;
import com.hustcad.plm.pdm.classification.service.TyplmClassificationService;
import cqrtplm.constant.RTErrorCodeEnum;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigInteger;

@Aspect
@Component
@Slf4j
public class ClassShearAOP {
    @Resource
    private TyplmClassificationService typlmClassificationService;

    @Around("execution(* com.hustcad.plm.pdm.classification.controller.TyplmClassificationController.cutToPrase(..))")
    @SneakyThrows
    public Object aroundSaveClassificationCode(ProceedingJoinPoint joinPoint) {
        // 获取方法参数
        Object[] args = joinPoint.getArgs();
        TyplmClassificationEntityDTO entityDTO = typlmClassificationService.queryClassificationByTypeAndName("",
                                                                                                             "ProductSpecification");
        TyplmClassificationQueryTreeDTO queryTreeDTO = new TyplmClassificationQueryTreeDTO();
        queryTreeDTO.setTypeOid(new BigInteger(entityDTO.getRootid()));
        queryTreeDTO.setOid(new BigInteger(entityDTO.getId()));
        TyplmClassificationEntityTreeVO parentClassTreeVO = this.typlmClassificationService.queryClassificationTree(
                queryTreeDTO).get(0);

        // 提取参数中的两个oid
        if (args.length > 0) {

            TyplmClassificationMoveParamDTO paramDTO = (TyplmClassificationMoveParamDTO) args[0];



            // 提取fromEntity的oid
            BigInteger fromOid = paramDTO.getFromEntity().getOid() != null ? paramDTO.getFromEntity().getOid() : null;

            // 提取toEntity的oid
            BigInteger toOid = paramDTO.getToEntity().getOid() != null ? paramDTO.getToEntity().getOid() : null;

            if (!displayName(fromOid, parentClassTreeVO)){
                throw  RTErrorCodeEnum.STATUS_INFO_CONTENT_IS_NULL.getException("不允许剪切产品线下的单个分类");
            }

            // 验证fromEntity的oid是否在产品型谱子节点内
            if (!isOidInProductSpecification(fromOid, parentClassTreeVO)) {
                // 直接跳出校验，继续执行原方法（不做拦截）
                return joinPoint.proceed();
            }

            // 验证toEntity的oid是否也在产品型谱子节点内
            if (!isOidInProductSpecification(toOid, parentClassTreeVO)) {
                throw  RTErrorCodeEnum.STATUS_INFO_CONTENT_IS_NULL.getException("不允许剪切至产品型谱以外的节点！！");
            }

        }

        // 继续执行原方法
        return joinPoint.proceed();
    }

    /**
     * 检查指定OID是否在产品型谱分类树的子节点内
     *
     * @param oid 要检查的OID
     * @param treeVO 分类树节点
     * @return 是否在子节点内
     */
    private boolean isOidInProductSpecification(BigInteger oid, TyplmClassificationEntityTreeVO treeVO) {
        if (oid == null || treeVO == null) {
            return false;
        }

        // 检查当前节点
        if (oid.equals(treeVO.getOid())) {
            return true;
        }

        // 递归检查子节点
        if (treeVO.getChildren() != null && !treeVO.getChildren().isEmpty()) {
            for (TyplmClassificationEntityTreeVO child : treeVO.getChildren()) {
                if (isOidInProductSpecification(oid, child)) {
                    return true;
                }
            }
        }

        return false;
    }

    private boolean displayName(BigInteger oid, TyplmClassificationEntityTreeVO treeVO) {
        if (oid == null || treeVO == null) {
            return false;
        }

        // 检查当前节点
        if (oid.equals(treeVO.getOid()) && treeVO.getDisplayName() != null && treeVO.getDisplayName().endsWith("组")) {
            return true;
        }

        // 递归检查子节点
        if (treeVO.getChildren() != null && !treeVO.getChildren().isEmpty()) {
            for (TyplmClassificationEntityTreeVO child : treeVO.getChildren()) {
                if (displayName(oid, child)) {
                    return true;
                }
            }
        }

        return false;
    }
}